﻿using System;
using System.ComponentModel;
using Loki.Utils;

namespace Luna.Model.Budget
{
    internal class EditableLine : Line, IEditableLine
    {
        #region Amount

        private static PropertyChangedEventArgs _AmountChangedArgs = ObservableHelper.CreateChangedArgs<EditableLine>(x => x.Amount);

        private static PropertyChangingEventArgs _AmountChangingArgs = ObservableHelper.CreateChangingArgs<EditableLine>(x => x.Amount);

        public decimal? Amount
        {
            get
            {
                return Data.Amount;
            }

            set
            {
                if (value != Data.Amount)
                {
                    NotifyChanging(_AmountChangingArgs);
                    Data.Amount = value;
                    NotifyChangedAndDirty(_AmountChangedArgs);
                }
            }
        }

        #endregion Amount

        private EditableBudget _Budget;

        public EditableLine(EditableBudget P_Budget)
            : base()
        {
            _Budget = P_Budget;
        }

        public EditableLine(EditableBudget P_Budget, Line P_Base)
            : base(P_Base)
        {
            _Budget = P_Budget;
        }

        public new IEditableBudget Budget
        {
            get { return _Budget; }
        }

        #region Category

        private static PropertyChangedEventArgs _CategoryChangedArgs = ObservableHelper.CreateChangedArgs<EditableLine>(x => x.Category);

        private static PropertyChangingEventArgs _CategoryChangingArgs = ObservableHelper.CreateChangingArgs<EditableLine>(x => x.Category);

        public new ICategory Category
        {
            get
            {
                return base.Category;
            }

            set
            {
                Guid? L_NewValue = null;
                if (value != null)
                {
                    L_NewValue = value.ID;
                }

                if (L_NewValue != Data.CategoryID)
                {
                    NotifyChanging(_CategoryChangingArgs);
                    Data.CategoryID = L_NewValue;
                    NotifyChangedAndDirty(_CategoryChangedArgs);
                }
            }
        }

        #endregion Category

        #region Code

        private static PropertyChangedEventArgs _CodeChangedArgs = ObservableHelper.CreateChangedArgs<EditableLine>(x => x.Code);

        private static PropertyChangingEventArgs _CodeChangingArgs = ObservableHelper.CreateChangingArgs<EditableLine>(x => x.Code);

        public new string Code
        {
            get
            {
                return Data.Code;
            }

            set
            {
                if (value != Data.Code)
                {
                    NotifyChanging(_CodeChangingArgs);
                    Data.Code = value;
                    NotifyChangedAndDirty(_CodeChangedArgs);
                }
            }
        }

        #endregion Code

        #region Description

        private static PropertyChangedEventArgs _DescriptionChangedArgs = ObservableHelper.CreateChangedArgs<EditableLine>(x => x.Description);

        private static PropertyChangingEventArgs _DescriptionChangingArgs = ObservableHelper.CreateChangingArgs<EditableLine>(x => x.Description);

        public new string Description
        {
            get
            {
                return Data.Description;
            }

            set
            {
                if (value != Data.Description)
                {
                    NotifyChanging(_DescriptionChangingArgs);
                    Data.Description = value;
                    NotifyChangedAndDirty(_DescriptionChangedArgs);
                }
            }
        }

        #endregion Description

        #region Name

        private static PropertyChangedEventArgs _NameChangedArgs = ObservableHelper.CreateChangedArgs<EditableLine>(x => x.Name);

        private static PropertyChangingEventArgs _NameChangingArgs = ObservableHelper.CreateChangingArgs<EditableLine>(x => x.Name);

        public new string Name
        {
            get
            {
                return Data.Name;
            }

            set
            {
                if (value != Data.Name)
                {
                    NotifyChanging(_NameChangingArgs);
                    Data.Name = value;
                    NotifyChangedAndDirty(_NameChangedArgs);
                }
            }
        }

        #endregion Name

        #region Parent

        private static PropertyChangedEventArgs _ParentChangedArgs = ObservableHelper.CreateChangedArgs<EditableLine>(x => x.Parent);

        private static PropertyChangingEventArgs _ParentChangingArgs = ObservableHelper.CreateChangingArgs<EditableLine>(x => x.Parent);

        public new IEditableLine Parent
        {
            get
            {
                return Data.ParentID.HasValue ? ((IEditableBudget)_Budget).Lines[Data.ParentID.Value] : null;
            }

            set
            {
                Guid? L_NewValue = null;
                if (value != null)
                {
                    L_NewValue = value.ID;
                }

                if (L_NewValue != Data.ParentID)
                {
                    NotifyChanging(_ParentChangingArgs);
                    Data.ParentID = L_NewValue;
                    NotifyChangedAndDirty(_ParentChangedArgs);
                }
            }
        }

        #endregion Parent
    }
}